my $loop_count = 0;

sub tarai {
    ++$loop_count;

    $_[1] < $_[0]
        ? tarai(tarai($_[0]-1, $_[1], $_[2]),
                tarai($_[1]-1, $_[2], $_[0]),
                tarai($_[2]-1, $_[0], $_[1]))
        : $_[1];

    #my ($x, $y, $z) = @_;
    #if ($y < $x) {
    #    return tarai(tarai($x-1, $y, $z),
    #                 tarai($y-1, $z, $x),
    #                 tarai($z-1, $x, $y));
    #} else {
    #    return $y;
    #}
}

print tarai(12, 6, 0), "\n";
print "$loop_count\n";
